package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.constant.BaseContextConstant;
import com.heima.constant.ClueStatus;
import com.heima.constant.ExcelConstant;
import com.heima.context.BaseContext;
import com.heima.dto.ClueDto;
import com.heima.dto.ClueSelectDto;
import com.heima.dto.FalseClueReasonDto;
import com.heima.dto.pagedto.CluePageDto;
import com.heima.entity.Clue;
import com.heima.mapper.ActivitiesMapper;
import com.heima.mapper.ClueMapper;
import com.heima.pojo.Activities;
import com.heima.result.PageResult;
import com.heima.service.ClueService;
import com.heima.vo.ClueVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ClueServiceImpl implements ClueService {
    private final ClueMapper clueMapper;
    private final ActivitiesMapper activitiesMapper;

    /**
     * 首次创建时, 新增字段内容
     * @param clue
     * @return
     */
    public Clue addFillFiled(Clue clue) {
        String username = (String) BaseContext.getData(BaseContextConstant.USERNAME);
        // 首次创建时 创建者 默认为 当前登录用户 (创建时间使用切面类生成)
        clue.setCreateBy(username);

        // 首次创建时 更新者 默认为 当前登录用户 (更新时间时间使用切面类生成)
        clue.setUpdateBy(username);

        // 首次创建时 跟踪状态 默认为 待跟踪的状态
        clue.setStatus(ClueStatus.TO_BE_FOLLOWED);

        // 首次创建时 线索拥有者 默认为为 当前用户
        clue.setOwner(BaseContext.getData(BaseContextConstant.USERNAME).toString());  // 首次创建时,默认的拥有者为 创建者

        // 首次创建时 拥有时间 默认为 当前时间
        clue.setOwnerTime(LocalDateTime.now());


        return clue;
    }

    @Override
    public void add(ClueDto clueDto) {
        // clueDto 中必填项为 phone(手机号) 和 channel(渠道来源)
        Clue clue = new Clue();
        BeanUtils.copyProperties(clueDto, clue);

        // 填充其他数据
        Clue clueNew = addFillFiled(clue);

        // 创建时间 与 更新时间 使用切面类添加
        clueMapper.add(clueNew);
    }

    @Override
    public PageResult pageQuery(CluePageDto cluePageDto) {
        PageHelper.startPage(cluePageDto.getPageNum(), cluePageDto.getPageSize());
        Page<Clue> page =  clueMapper.pageQuery(cluePageDto);
        List<ClueVo> clueVoList = new ArrayList<>();
        for (Clue clue : page) {
            ClueVo clueVo = new ClueVo();
            BeanUtils.copyProperties(clue, clueVo);
            clueVoList.add(clueVo);
        }
        PageResult pageResult = new PageResult();
        pageResult.setTotal((long) clueVoList.size());
        pageResult.setRows(clueVoList);
        return pageResult;
    }

    @Override
    public void addBatch(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        XSSFWorkbook excel = new XSSFWorkbook(inputStream);
        XSSFSheet sheet = excel.getSheet(ExcelConstant.SHEET_NAME);

        int maxRow = sheet.getLastRowNum();
        int maxColumn = ExcelConstant.MAX_COLUMN;

        List<Clue> clueList = new ArrayList<>();
        for (int i = 1; i <= maxRow; i++) {
            Clue clue = new Clue();

            for (int j = 0; j < maxColumn; j++) {
                // 获取 cell 对象
                XSSFCell cell = sheet.getRow(i).getCell(j);
                switch (j){
                    case 0 -> clue.setPhone(cell.getStringCellValue());
                    case 1 -> clue.setChannel(Double.valueOf(cell.getNumericCellValue()).intValue());
                    case 2 -> clue.setActivityId(Double.valueOf(cell.getNumericCellValue()).longValue());
                    case 3 -> clue.setName(cell.getStringCellValue());
                    case 4 -> clue.setSex(Double.valueOf(cell.getNumericCellValue()).intValue());
                    case 5 -> clue.setAge(Double.valueOf(cell.getNumericCellValue()).intValue());
                    case 6 -> clue.setWeixin(cell.getStringCellValue());
                    case 7 -> clue.setQq(cell.getStringCellValue());
                }
            }
            Clue clueNew = addFillFiled(clue);
            clueList.add(clueNew);
        }

        inputStream.close();
        excel.close();
        clueMapper.addBatch(clueList);

    }

    @Override
    public List<Activities> queryActivityListByChannelId(Integer channelId) {
        List<Activities> activityList = activitiesMapper.getActivityByChannel(channelId);
        return activityList;
    }

    @Override
    public ClueVo queryById(Integer id) {
        Clue clue = clueMapper.queryById(id);
        ClueVo clueVo = new ClueVo();
        BeanUtils.copyProperties(clue, clueVo);
        return clueVo;
    }

    @Override
    public void falseClueSetting(Integer id, FalseClueReasonDto reason) {
        // todo 伪线索信息应该如何处理
        clueMapper.falseClueSetting(id);
    }

    @Override
    public void record(ClueDto clueDto) {
        Clue clue = new Clue();
        BeanUtils.copyProperties(clueDto, clue);
        clue.setId(clueDto.getClueId());
        clueMapper.record(clue);
    }

    @Override
    public PageResult poolPageQuery(CluePageDto cluePageDto, String begin, String end) {
        PageHelper.startPage(cluePageDto.getPageNum(), cluePageDto.getPageSize());
        Page<Clue> page =  clueMapper.poolPageQuery(cluePageDto);
        List<ClueVo> clueVoList = new ArrayList<>();
        for (Clue clue : page) {
            ClueVo clueVo = new ClueVo();
            BeanUtils.copyProperties(clue, clueVo);
            clueVoList.add(clueVo);
        }
        PageResult pageResult = new PageResult();
        pageResult.setTotal((long) clueVoList.size());
        pageResult.setRows(clueVoList);
        return pageResult;
    }

    @Override
    public void batchGain(ArrayList<Integer> ids) {
        clueMapper.batchGain(ids);
    }

    @Override
    public void changeBusiness(Integer id) {
        Clue clue = clueMapper.queryById(id);
        // todo 转商机

    }

    @Override
    public void assignment(ClueSelectDto clueSelectDto) {
        // 线索分配
        // 设置分配者与分配时间, 分配者为 当前操作用户, 分配时间为 当前时间
        // 设置所属者和所属时间, 所属者 clueSelectDto.userId , 所属时间为 当前时间
        String assignBy = BaseContext.getData(BaseContextConstant.USERNAME).toString();
        clueMapper.assignment(clueSelectDto, assignBy);
    }


}
